package com.ruoyi.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.user.domain.UserChdjResult;
import com.ruoyi.user.domain.bo.UserChdjResultBo;
import com.ruoyi.user.domain.vo.UserChdjResultVo;
import com.ruoyi.user.mapper.UserChdjResultMapper;
import com.ruoyi.user.service.IUserChdjResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 查获登记结果Service业务层处理
 *
 * @author strongme
 * @date 2023-01-26
 */
@RequiredArgsConstructor
@Service
public class UserChdjResultServiceImpl implements IUserChdjResultService {

    private final UserChdjResultMapper baseMapper;

    /**
     * 查询查获登记结果
     */
    @Override
    public UserChdjResultVo queryById(Long chdjResultId) {
        return baseMapper.selectVoById(chdjResultId);
    }

    @Override
    public List<UserChdjResultVo> queryByChdjId(Long chdjId) {
        return baseMapper.selectVoList(Wrappers.<UserChdjResult>lambdaQuery().eq(UserChdjResult::getUserChdjId, chdjId));
    }

    /**
     * 查询查获登记结果列表
     */
    @Override
    public TableDataInfo<UserChdjResultVo> queryPageList(UserChdjResultBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserChdjResult> lqw = buildQueryWrapper(bo);
        Page<UserChdjResultVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询查获登记结果列表
     */
    @Override
    public List<UserChdjResultVo> queryList(UserChdjResultBo bo) {
        LambdaQueryWrapper<UserChdjResult> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserChdjResult> buildQueryWrapper(UserChdjResultBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserChdjResult> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserChdjId()), UserChdjResult::getUserChdjId, bo.getUserChdjId());
        lqw.eq(bo.getSj() != null, UserChdjResult::getSj, bo.getSj());
        lqw.eq(StringUtils.isNotBlank(bo.getDd()), UserChdjResult::getDd, bo.getDd());
        lqw.eq(StringUtils.isNotBlank(bo.getJg()), UserChdjResult::getJg, bo.getJg());
        return lqw;
    }

    /**
     * 新增查获登记结果
     */
    @Override
    public Boolean insertByBo(UserChdjResultBo bo) {
        UserChdjResult add = BeanUtil.toBean(bo, UserChdjResult.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setChdjResultId(add.getChdjResultId());
        }
        return flag;
    }

    /**
     * 修改查获登记结果
     */
    @Override
    public Boolean updateByBo(UserChdjResultBo bo) {
        UserChdjResult update = BeanUtil.toBean(bo, UserChdjResult.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserChdjResult entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除查获登记结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
